package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestHeader;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.system.annotation.ContentSecurity;
import com.hello.system.domain.TopicArticles;
import com.hello.system.service.ITopicArticlesService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 专题文章Controller
 * 
 * @author hello
 * @date 2025-05-30
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/articles")
public class TopicArticlesController extends BaseController
{
    private final ITopicArticlesService topicArticlesService;

    /**
     * 查询专题文章列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TopicArticles topicArticles)
    {
        startPage();
        List<TopicArticles> list = topicArticlesService.selectTopicArticlesList(topicArticles);
        return getDataTable(list);
    }

    /**
     * 导出专题文章列表
     */
    @Log(title = "专题文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TopicArticles topicArticles)
    {
        List<TopicArticles> list = topicArticlesService.selectTopicArticlesList(topicArticles);
        ExcelUtil<TopicArticles> util = new ExcelUtil<TopicArticles>(TopicArticles.class);
        util.exportExcel(response, list, "专题文章数据");
    }

    /**
     * 获取专题文章详细信息
     */
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(topicArticlesService.selectTopicArticlesByArticleId(articleId));
    }

    /**
     * 新增专题文章
     */
    @Log(title = "专题文章", businessType = BusinessType.INSERT)
    @PostMapping
    @ContentSecurity(fieldName = "content")
    public AjaxResult add(@RequestBody TopicArticles topicArticles, @RequestHeader(value = "X-WX-OPENID", required = false) String openid)
    {
        // 内容安全检测由@ContentSecurity注解处理，通过请求头获取openid
        log.info("创建专题文章，openid: {}", openid);
        
        // 标题也需要检测内容安全
        if (topicArticles.getTitle() != null && !topicArticles.getTitle().isEmpty()) {
            topicArticles.setContent(topicArticles.getTitle() + "\n" + topicArticles.getContent());
        }
        
        int rows = topicArticlesService.insertTopicArticles(topicArticles);
        if (rows > 0) {
            // 获取完整的文章对象（包含ID等信息）
            TopicArticles newArticle = topicArticlesService.selectTopicArticlesByArticleId(topicArticles.getArticleId());
            AjaxResult result = AjaxResult.success("发布成功");
            result.put("data", newArticle);
            return result;
        }
        
        return error("发布失败");
    }

    /**
     * 修改专题文章
     */
    @Log(title = "专题文章", businessType = BusinessType.UPDATE)
    @PutMapping
    @ContentSecurity(fieldName = "content")
    public AjaxResult edit(@RequestBody TopicArticles topicArticles, @RequestHeader(value = "X-WX-OPENID", required = false) String openid)
    {
        // 内容安全检测由@ContentSecurity注解处理，通过请求头获取openid
        log.info("更新专题文章，openid: {}", openid);
        
        // 标题也需要检测内容安全
        if (topicArticles.getTitle() != null && !topicArticles.getTitle().isEmpty()) {
            topicArticles.setContent(topicArticles.getTitle() + "\n" + topicArticles.getContent());
        }
        
        return toAjax(topicArticlesService.updateTopicArticles(topicArticles));
    }

    /**
     * 删除专题文章
     */
    @Log(title = "专题文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(topicArticlesService.deleteTopicArticlesByArticleIds(articleIds));
    }
}
